package com.Dao.impl;

import com.Dao.StudentDao;
import com.domain.Student;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;

public class StudentDaoimpl implements StudentDao {
    @Override
    public List<Student> findAll() {
        List<Student> all = null;
        StudentDao mapper;
        SqlSession sqlSession;
        SqlSessionFactory build;
        try {
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            build = new SqlSessionFactoryBuilder().build(is);
            sqlSession = build.openSession();
            mapper = sqlSession.getMapper(StudentDao.class);
            all = mapper.findAll();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return all;
    }

    @Override
    public Student findById(Integer id) {
        StudentDao mapper;
        SqlSession sqlSession;
        SqlSessionFactory build;
        Student byId = null;
        try {
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            build = new SqlSessionFactoryBuilder().build(is);
            sqlSession = build.openSession();
            mapper = sqlSession.getMapper(StudentDao.class);
            byId = mapper.findById(id);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return byId;
    }

    @Override
    public int insert(Student stu) {
        StudentDao mapper;
        SqlSession sqlSession;
        SqlSessionFactory build;
        int insert = 0;
        try {
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            build = new SqlSessionFactoryBuilder().build(is);
            sqlSession = build.openSession();
            mapper = sqlSession.getMapper(StudentDao.class);
            insert = mapper.insert(stu);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return insert;
    }

    @Override
    public int update(Student stu) {
        StudentDao mapper;
        SqlSession sqlSession;
        SqlSessionFactory build;
        int update = 0;
        try {
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            build = new SqlSessionFactoryBuilder().build(is);
            sqlSession = build.openSession(true);
            mapper = sqlSession.getMapper(StudentDao.class);
            update = mapper.update(stu);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return update;
    }

    @Override
    public int delete(Integer id) {
        StudentDao mapper;
        SqlSession sqlSession;
        SqlSessionFactory build;
        int delete = 0;
        try {
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            build = new SqlSessionFactoryBuilder().build(is);
            sqlSession = build.openSession(true);
            mapper = sqlSession.getMapper(StudentDao.class);
            delete = mapper.delete(id);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return delete;
    }

    @Override
    public List<Student> Condition(Student stu) {
        StudentDao mapper;
        SqlSession sqlSession;
        SqlSessionFactory build;
        List<Student> condition =null;
        try {
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            build = new SqlSessionFactoryBuilder().build(is);
            sqlSession = build.openSession(true);
            mapper = sqlSession.getMapper(StudentDao.class);
            condition = mapper.Condition(stu);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return condition;
    }

    @Override
    public List<Student> FindAlllimt() {
        List<Student> all = null;
        StudentDao mapper;
        SqlSession sqlSession;
        SqlSessionFactory build;
        try {
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            build = new SqlSessionFactoryBuilder().build(is);
            sqlSession = build.openSession();
            PageHelper.startPage(1,3);
            mapper = sqlSession.getMapper(StudentDao.class);
            all = mapper.findAll();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return all;
    }
}
